<template>
    <el-form :model="formDate" status-icon :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="所属学科" prop="subjectID">
        <el-select v-model="formDate.subjectID">
          <el-option v-for="item in subjectData" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="formDate.directoryName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="enterFn">确定</el-button>
      </el-form-item>
    </el-form>
</template>

<script>
export default {
  props: {
    // 目录列表-数据
    formData: {
      type: Array,
      default: _ => []
    },
    subjectData: {
      type: Array,
      default: _ => []
    }
  },
  data () {
    return {
      // 添加表单字段
      formDate: {
        subjectID: '', // 所属学科
        directoryName: '' // 目录名称
      },
      // 添加表单校验字段
      rules: {
        subjectID: [
          { required: true, message: '所属学科不能为空', trigger: 'blur' }
        ],
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' },
          { min: 1, max: 7, message: '长度不得超过7位', trigger: 'blur' }
        ]
      },
      showForm: false
    }
  },
  methods: {
    cancelFn () {
      this.$emit('update:showDialog', false)
      this.formDate = {
        subjectID: '',
        directoryName: ''
      }
    },
    enterFn () {
      this.$refs.addForm.validate(valid => {
        if (valid === false) {
          this.$emit('update:showDialog', true)
          this.formDate = {
            subjectID: '',
            directoryName: ''
          }
        } else {
          this.$emit('enterEV', { ...this.formDate })
          this.$emit('update:showDialog', false)
          this.formDate = {
            subjectID: '',
            directoryName: ''
          }
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
